﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace Faust.Andrew.MTG
{
	/// <summary>
	/// Interaction logic for DeckWindow.xaml
	/// </summary>
	public partial class DeckWindow : Window
	{
        private DataStore _dataStore;
        private DragDropManager _dragManager;
        private Gatherer _gatherer;

        public event EventHandler<DeckEventArgs> DeckSavedEvent;
        protected void FireDeckSavedEvent()
        {
            if (DeckSavedEvent != null)
            {
                DeckSavedEvent(this, new DeckEventArgs(DeckName));
            }
        }

        public event EventHandler<DeckEventArgs> DeckClosedEvent;
        protected void FireDeckClosedEvent()
        {
            if (DeckClosedEvent!= null)
            {
                DeckClosedEvent(this, new DeckEventArgs(DeckName));
            }
        }

        public string DeckName
        {
            get { return _deck.Name; }
        }

        public DeckWindow(string deckName, Gatherer gatherer, DataStore dataStore, DragDropManager dragManager)
            : this()
        {
            _dragManager = dragManager;
            
            _gatherer = gatherer;
            _dataStore = dataStore;

            LoadDeck(deckName);
        }        

        private Deck _deck;
        public Deck Deck
        {
            get { return _deck; }
            set { SetDeck(value); }
        }

        public void Refresh()
        {
            //lbDeck.ItemsSource = null;
            //lbDeck.ItemsSource = _deck.Cards;
        }

        private void SetDeck(Deck deck)
        {
            LoadDeck(deck);
        }

        private void LoadDeck(string deckName)
        {

            Deck deck = _dataStore.GetDeck(deckName);

            if (deck == null)
            {
                deck = new Deck();
                deck.Name = System.IO.Path.GetRandomFileName();
            }

            LoadDeck(deck);
        }

        private void LoadDeck(Deck deck)
        {
            if (deck == null)
                throw new ArgumentNullException("deck", "deck is null.");

            _deck = deck;           
            _deck.IsChanged = false;

            DataContext = _deck;

            //lbDeck.ItemsSource = _deck.Cards;

            //txtDeckName.DataContext = _deck;
            //txtDeckDescription.DataContext = _deck;
            

            Title = String.Format("Deck: {0}", _deck.Name);

            deckStatistics.Deck = _deck;
        }


		public DeckWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}

		private void cmbSort_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
            if (_deck == null) { return; }

            ListBoxItem item = cmbSort.SelectedItem as ListBoxItem;
            if (item == null || item.Content == null) { return; }

            string sortBy = item.Content.ToString();
            DeckCardHelper.Sort(_deck.Cards, sortBy);
           
            //lbDeck.ItemsSource = null;
            //lbDeck.ItemsSource = _deck.Cards;
		}

        public new void Activate()
        {
            this.Show();
        }

        private void Card_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _dragManager.PreviewMouseLeftButtonDown((FrameworkElement)sender, e, false);
        }

        private void Card_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            _dragManager.PreviewMouseMove((FrameworkElement)sender, e, this);
        }

        private void Deck_PreviewDrop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Card)))
            {
                e.Handled = true;
                Card card = (Card)e.Data.GetData(typeof(Card));
                this.Deck.SetCardQuantity(card, 1, true);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_deck != null && _deck.IsChanged)
            {
                MessageBoxResult result = MessageBox.Show("Do you wish to save changes to the deck?", "Save Changes", MessageBoxButton.YesNoCancel);
                if (result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }
                else if (result == MessageBoxResult.Yes)
                {
                    SaveDeck();
                }
            }

            FireDeckClosedEvent();
        }

        private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SaveDeck();
        }

        private void Close_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Close();
        }

        private void SaveDeck()
        {
            if (_deck == null)
            {
                return;
            }

            if (_deck.DeckID < 1)
            {
                _deck.DeckID = _dataStore.CreateDeck(_deck.Name, _deck.Description).DeckID;
            }

            _dataStore.UpdateDeck(_deck, true);
            FireDeckSavedEvent();
        }
    }
}